package org.csu.code.api.controller;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
import org.csu.code.api.common.CommonResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.UUID;

import com.google.code.kaptcha.impl.DefaultKaptcha;

@Controller
@RequestMapping("/kaptcha/")
public class KaptchaController {

    private final Logger logger = LoggerFactory.getLogger(KaptchaController.class);

    /**
     * 验证码工具
     */
    @Autowired
    DefaultKaptcha defaultKaptcha;

    /**
     * redis工具
     */
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 生成验证码
     *
     * @param httpServletRequest  获取token
     * @param httpServletResponse 传输图片
     * @throws Exception
     */
    @GetMapping("img")
    @ResponseBody
    public CommonResponse<Map> img(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)
            throws Exception {
        byte[] captchaChallengeAsJpeg = null;
        ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream();
        try {
            String createText = defaultKaptcha.createText();
            // 生产验证码字符串并保存到 Redis 中，ip-rightCode，有效期为 1 小时
            String token = UUID.randomUUID().toString();
//            String ip = httpServletRequest.getRemoteAddr();
            logger.info("token：" + token + "，rightCode = " + createText);
            redisTemplate.opsForValue().set(token, createText, 1, TimeUnit.HOURS);
            // 使用生产的验证码字符串返回一个BufferedImage对象并转为byte写入到byte数组中
            BufferedImage challenge = defaultKaptcha.createImage(createText);
            ImageIO.write(challenge, "jpg", jpegOutputStream);
            String base64bytes = Base64.encode(jpegOutputStream.toByteArray());

            //该字符串传输至前端放入src即可显示图片，安卓可以去掉data:image/png;base64,
            String src = "data:image/jpg;base64," + base64bytes;
            Map<String, Object> map = new HashMap<>(2);
            map.put("token", token);
            map.put("img", src);
            return CommonResponse.createForSuccess(map);
        } catch (IllegalArgumentException e) {
            httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND);
            return null;
        }

        // 定义response输出类型为image/jpeg类型，使用response输出流输出图片的byte数组
//        captchaChallengeAsJpeg = jpegOutputStream.toByteArray();
//        httpServletResponse.setHeader("Cache-Control", "no-store");
//        httpServletResponse.setHeader("Pragma", "no-cache");
//        httpServletResponse.setDateHeader("Expires", 0);
//        httpServletResponse.setContentType("image/jpeg");
//        ServletOutputStream responseOutputStream = httpServletResponse.getOutputStream();
//        responseOutputStream.write(captchaChallengeAsJpeg);
//        responseOutputStream.flush();
//        responseOutputStream.close();
    }

    /**
     * 校对验证码
     *
     * @param httpServletRequest 获取token
     * @return
     */
    @ResponseBody
    @GetMapping("check/{tryCode}")
    public CommonResponse<String> check(HttpServletRequest httpServletRequest, @PathVariable String tryCode) {
//        String ip = httpServletRequest.getRemoteAddr();
        String token = httpServletRequest.getHeader("token");
        logger.info("token：" + token + "，tryCode = " + tryCode);
        // 从 Redis 中校验
        String rightCode = redisTemplate.opsForValue().get(token);
        if (rightCode != null && rightCode.equals(tryCode)) {
            return CommonResponse.createForSuccess("校验成功");
        }
        return CommonResponse.createForError("校验失败");
    }
}

